<template>
    <el-main class="code">
        <el-main class="ep-body">
            <el-card class="ep-card">
                <el-form ref="form" :model="form">
                    <el-row>
                        <el-col :span="8">
                            <el-form-item label="代码类别" label-width="120px">
                                <el-input v-model="form.aaa100" type="text" name="aaa100" placeholder="请输入代码类别"></el-input>
                            </el-form-item>
                        </el-col>
                        <el-col :span="8">
                            <el-form-item label="代码名称" label-width="120px">
                                <el-input v-model="form.aaa103" type="text" name="aaa103" placeholder="请输入代码名称"></el-input>
                            </el-form-item>
                        </el-col>
                        <el-col :span="8">
                            <el-button style="float: right;margin-right: 10px" type="primary" size="small" @click="query(true)">查询</el-button>
                            <el-button style="float: right;margin-right: 10px" type="primary" size="small" @click="handleAdd">新增</el-button>
                            <!--<el-button style="float: right;margin-right: 10px" type="info" size="small" @click="TestFilter">测试</el-button>-->
                        </el-col>
                    </el-row>
                </el-form>
            </el-card>

            <el-card class="ep-card">
                <el-row>
                    <el-table :data="tableData.data" border style="width: 100%;" height="calc(100vh - 310px)" @row-dblclick="handleEdit('', $event)" isUpdate>
                        <!--<el-table-column type="selection" width="55" align="center"></el-table-column>-->
                        <el-table-column type="index" align="center" width="30px" label="序号"></el-table-column>
                        <el-table-column sortable align="center" prop="aaa100" label="代码类别"></el-table-column>
                        <el-table-column sortable align="center" prop="aaa102" label="代码值"></el-table-column>
                        <el-table-column sortable align="center" prop="aaa103" label="代码名称"></el-table-column>
                        <el-table-column sortable align="center" prop="aaa105" label="参数分类"></el-table-column>
                        <el-table-column sortable align="center" prop="aae100" label="有效标志">
                            <template slot-scope="scope">
                                <!--<el-select v-model="tableData.data[scope.$index].aae100">
                                    <el-option v-for="item in codeTypes.AAE100" :key="item.key" :label="item.value" :value="item.key">
                                    </el-option>
                                </el-select>-->
                                <div v-for="aae100 in codeTypes.AAE100">
                                    <span v-if="aae100.key === scope.row.aae100">
                                        {{aae100.value}}
                                    </span>
                                </div>
                                <!--{{codeTypes.AAE100[scope.row.aae100]}}-->
                            </template>
                        </el-table-column>
                        <el-table-column sortable align="center" prop="aaa104" label="代码可维护标志">
                            <template slot-scope="scope">
                                <!--<el-select v-model="tableData.data[scope.$index].aaa104">
                                    <el-option v-for="item in codeTypes.AAA104" :key="item.key" :label="item.value" :value="item.key">
                                    </el-option>
                                </el-select>-->
                                <div v-for="aaa104 in codeTypes.AAA104">
                                    <span v-if="aaa104.key === scope.row.aaa104">
                                        {{aaa104.value}}
                                    </span>
                                </div>
                                <!--{{codeTypes.AAA104[scope.row.aaa104].value}}-->
                            </template>
                        </el-table-column>
                        <el-table-column align="center" prop="op" label="操作">
                            <template slot-scope="scope">
                                <el-button type="text" size="mini" style="padding: 7px 0px;" @click="handleEdit(scope.$index, scope.row)">修改</el-button>
                                <el-button type="text" size="mini" style="padding: 7px 0px;" @click="handleDelete(scope.$index, scope.row)">删除</el-button>
                            </template>
                        </el-table-column>
                    </el-table>
                    <el-pagination
                        style="text-align: right;"
                        @size-change="handleSizeChange"
                        @current-change="handleCurrentChange"
                        :current-page.sync="tableData.page"
                        :page-sizes="[10, 20, 50, 100]"
                        :page-size="10"
                        background
                        layout="total, sizes, prev, pager, next, jumper"
                        :total="tableData.total">
                    </el-pagination>
                </el-row>
            </el-card>
        </el-main>
        <el-dialog :title="dialogFormTitle" :visible.sync="dialogFormVisible" width="700px" top="15vh" close-on-press-escape>
            <el-form :model="codeForm" name="codeForm" ref="codeForm"  :rules="rules">
                <el-row>
                    <el-col :span="12">
                        <el-form-item label="代码类别" label-width="100px" prop="aaa100">
                            <el-input v-model="codeForm.aaa100" type="text" name="aaa100" placeholder="请输入代码类别"></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="代码值" label-width="100px" prop="aaa102">
                            <el-input v-model="codeForm.aaa102" type="text" name="aaa102" placeholder="请输入代码值"></el-input>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="12">
                        <el-form-item label="代码名称" label-width="100px" prop="aaa103">
                            <el-input v-model="codeForm.aaa103" type="text" name="aaa103" placeholder="请输入代码名称"></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="是否有效" label-width="100px" prop="aae100">
                            <el-select v-model="codeForm.aae100" style="width: 100%">
                                <el-option v-for="item in codeTypes.AAE100" :key="item.key" :label="item.value" :value="item.key">
                                </el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="12">
                        <el-form-item label="参数分类" label-width="100px">
                            <el-input v-model="codeForm.aaa105" type="text" name="aaa105" placeholder="请输入参数分类"></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="是否可维护" label-width="100px" prop="aaa104">
                             <el-select v-model="codeForm.aaa104" style="width: 100%">
                                 <el-option v-for="item in codeTypes.AAA104" :key="item.key" :label="item.value" :value="item.key">
                                 </el-option>
                             </el-select>
                        </el-form-item>
                    </el-col>
                </el-row>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="dialogFormVisible = false">取 消</el-button>
                <el-button type="primary" @click="save">确 定</el-button>
            </div>
        </el-dialog>
    </el-main>
</template>

<script type="es6">
    export default {
        data() {
            return {
                form: {
                    aaa100: '',
                    aaa103: ''
                },
                tableData: {
                    data: [],
                    total: 0,
                    page: 1,
                    size: 10
                },
                codeForm: {
                    aaz093: '',
                    aaa100: '',
                    aaa102: '',
                    aaa103: '',
                    aaa105: '',
                    aae100: '',
                    aaa104: ''
                },
                codeTypes: {
                    AAE100: [{
                        key: '1',
                        value: '有效'
                    }, {
                        key: '0',
                        value: '无效'
                    }],
                    AAA104: [{
                        key: '1',
                        value: '可维护'
                    }, {
                        key: '0',
                        value: '不可维护'
                    }]
                },
                rules: {
                    aaa100: [
                        { required: true, message: '请输入代码类型', trigger: 'blur' }
                    ],
                    aaa102: [
                        { required: true, message: '请输入代码值', trigger: 'blur' }
                    ],
                    aaa103: [
                        { required: true, message: '请输入代码名称', trigger: 'blur' }
                    ],
                    aae100: [
                        { required: true, message: '请选择有效状态', trigger: 'blur' }
                    ],
                    aaa104: [
                        { required: true, message: '请选择可维护状态', trigger: 'blur' }
                    ]
                },
                dialogFormTitle: '新增代码',
                dialogFormVisible: false,
                path: this.$route.path
            }
        },
        mounted() {
            this.initCodeTypes();
        },
        methods: {
            initCodeTypes() {
                let params = this.codeTypes;
                this.$api.commonPost(this.path + "/initCodeTypes", params).then(res => {
                    this.handleMessage(res);
                    if(res.status === "0") {
                        console.info(res);
                        this.codeTypes = res.data;
                        console.info(this.codeTypes);
                    }
                });
            },
            query(isClickQueryBtn) {
                if(isClickQueryBtn) {
                    this.tableData.page = 1;
                }
                let params = {aaa100:this.form.aaa100, aaa103:this.form.aaa103, page:this.tableData.page - 1, size:this.tableData.size};
                this.$api.commonGet(this.path + "/query", params).then(res => {
                    this.handleMessage(res);
                    if(res.status === "0") {
                        this.tableData.total = res.data.total;
                        this.tableData.data = res.data.data;
                    }
                });
            },
            save() {
                // 表单校验
                this.$refs["codeForm"].validate((valid) => {
                    if (valid) {
                        let params = this.codeForm;
                        this.$api.commonPost(this.path + "/save", params).then(res => {
                            this.handleMessage(res);
                            if(res.status === "0") {
                                this.dialogFormVisible = false;
                                if(this.codeForm.aaz093 != null && this.codeForm.aaz093 != '') {
                                    this.query();
                                }
                            }
                        });
                    } else {
                        console.log('error submit!!');
                        return false;
                    }
                });
            },
            delete() {
                let params = this.codeForm;
                this.$api.commonPost(this.path + "/delete", params).then(res => {
                    this.handleMessage(res);
                    if(res.status === "0") {
                        this.query();
                    }
                });
            },
            handleAdd() {
                this.dialogFormTitle = '新增代码';
                this.codeForm = {};
                this.dialogFormVisible = true;
            },
            handleSizeChange(val) {
                console.log(`每页 ${val} 条`);
                this.tableData.size = val;
                if(this.tableData.total !== 0) {
                    this.query();
                }
            },
            handleCurrentChange(val) {
                console.log(`当前页: ${val}`);
                this.tableData.page = val;
                this.query();
            },
            handleEdit(index, row) {
                console.log(index, row);
                this.dialogFormTitle = '修改代码';
                this.codeForm = JSON.parse(JSON.stringify(row));
                this.dialogFormVisible = true;
            },
            handleDelete(index, row) {
                console.log(index, row);
                this.$confirm('是否删除该代码?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    this.codeForm = JSON.parse(JSON.stringify(row));
                    this.delete();
                }).catch(() => {
                    this.$message.success("已取消删除");
                });
            },
            handleMessage(res) {
                if (res.status === '0') {
                    if (res.message !== null) {
                        this.$message.success(res.message);
                    }
                } else if (res.status === '1' && res.message !== null) {
                    this.$message.warning(res.message);
                } else if (res.status === '9' && res.message !== null) {
                    this.$message.error(res.message);
                }
            },
            /*TestFilter() {
                let params = {
                    page: 1,
                    size: 5,
                    selectFilterData: window.btoa("(aaa105='TEST')")
                };
                this.$api.commonPost("/codetype/TEST/listfilter", params).then(res => {
                    this.handleMessage(res);
                    if(res.status === "0") {
                        console.info(res.data);
                    }
                });
            }*/
        }
    }
</script>

<style lang="less" scoped>
    .code /deep/ .cell {
        padding-left: 0px !important;
        padding-right: 0px !important;
    }
</style>
